package household.user.mvc;

import household.user.model.User;
import household.user.service.UserService;
import household.util.IModelAndView;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/user")
public class UserController{
	
	@Autowired
	public UserService userServiceImpl;
	
	@RequestMapping("/login.htm")
	public IModelAndView login(@Valid User user,BindingResult result){
		int usrID = 0;
		if(!result.hasErrors()){
			usrID =userServiceImpl.save(user);
		}		
		IModelAndView mav = new IModelAndView("jsonView",usrID);
		System.out.println("login");
		return mav;
		
	}
	@RequestMapping("/get.htm")
	public IModelAndView login(int id){		
		User user = userServiceImpl.get(id);	
		IModelAndView mav = new IModelAndView("jsonView",user);
		System.out.println("get:"+user.getId());
		return mav;
		
	}

}
